//
//  AddCategoryViewController.m
//  quickBatchManager
//
//  Created by jack on 15/9/23.
//  Copyright (c) 2015年 jack. All rights reserved.
//

#import "AddCategoryViewController.h"
@interface AddCategoryViewController ()

@end

@implementation AddCategoryViewController
@synthesize descTextView,exitBtn,comfirmBtn,categoryTextField,bgView,contentView;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

-(void)initUI
{
    self.view.backgroundColor=[UIColor clearColor];
   
    
    descTextView.layer.borderColor=[UIColor colorWithWhite:0.85 alpha:1].CGColor;
    descTextView.layer.borderWidth=0.5;
    descTextView.layer.cornerRadius=5;
    descTextView.clipsToBounds=YES;
    descTextView.backgroundColor=[UIColor whiteColor];
    
    
    
    if(_category)
    {
        descTextView.text=_category.categoryDesc;
        categoryTextField.text=_category.categoryName;
        _delBtn.hidden=NO;
    }
    else _delBtn.hidden=YES;
    
    
    [_delBtn bk_whenTapped:^{
         [_category deleteToDB];
        if([_delegate respondsToSelector:@selector(didChange:andData:)])
        {
            [_delegate didChange:ChangeTypeCategory andData:nil];
        }
         [self dismissViewControllerAnimated:YES completion:nil];
    }];
    
    
    
    
    
    
    UIImage *image = bgView.image;
    image = [image stretchableImageWithLeftCapWidth:floorf(image.size.width/2) topCapHeight:floorf(image.size.height/2)];
    bgView.image=image;
    
    [contentView sendSubviewToBack:bgView];
    [exitBtn bk_whenTapped:^{
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    
    [_closeBtn bk_whenTapped:^{
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    
    [comfirmBtn bk_whenTapped:^{
        if(![categoryTextField.text isBlank])
        {
            CategoryModel *model = [[CategoryModel alloc] init];
            model.categoryName = categoryTextField.text;
            if([descTextView.text isBlank])
                model.categoryDesc = model.categoryName;
            else
                model.categoryDesc = descTextView.text;
            
            
            if(_category)
            {
                model.categoryId=_category.categoryId;
                [model updateToDB];
            }
            else [CategoryModel insertToDB:model];
            [self dismissViewControllerAnimated:YES completion:nil];
             if([_delegate respondsToSelector:@selector(didChange:andData:)])
             {
                  [_delegate didChange:ChangeTypeCategory andData:nil];
             }
           
          
        }else
        {
            
        }
        
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
